package cn.icatw.admin.controller;

import cn.icatw.admin.common.Const;
import cn.icatw.admin.common.R;
import cn.icatw.admin.domain.User;
import cn.icatw.admin.service.UserService;
import cn.icatw.admin.utils.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 前台壁纸用户表(User)表控制层
 *
 * @author icatw
 * @since 2022-10-14 19:58:57
 */
@Api(tags = "前台壁纸用户表(User)")
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    @Lazy
    BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询所有数据
     */
    @ApiOperation(value = "分页查询所有数据 前台壁纸用户表")
    @GetMapping
    public R page(@RequestParam(required = false) String nickname, @RequestParam int current, @RequestParam int size) {
        Page<User> page = new Page<>(current, size);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(nickname)) {
            wrapper.like("nickname", nickname);
        }
        return R.ok(this.userService.page(page, wrapper));
    }


    /**
     * 通过主键查询单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据 前台壁纸用户表")
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.userService.getById(id));
    }

    /**
     * 新增数据
     */
    @ApiOperation(value = "新增数据 前台壁纸用户表")
    @PostMapping("/save")
    public R save(@RequestBody User user) {
        long count = userService.count(new QueryWrapper<User>().eq("account", user.getAccount()));
        if (count>0){
            return R.fail("此账号已存在!");
        }
        //初始密码
        String initialPassword = MD5Util.string2MD5(Const.INITIAL_PASSWORD);
        //String initialPassword = bCryptPasswordEncoder.encode(Const.INITIAL_PASSWORD);
        user.setPassword(initialPassword);
        user.setAvatar(Const.INITIAL_AVATAR);
        user.setSign(Const.INITIAL_SIGN);
        return R.ok(this.userService.save(user));
    }

    /**
     * 修改数据
     */
    @ApiOperation(value = "修改数据 前台壁纸用户表")
    @PostMapping("/update")
    public R updateById(@RequestBody User user) {
        long count = userService.count(new QueryWrapper<User>().eq("account", user.getAccount()));
        if (count>0){
            return R.fail("此账号已存在!");
        }
        return R.ok(this.userService.updateById(user));
    }

    /**
     * 单条/批量删除数据
     */
    @ApiOperation(value = "单条/批量删除数据 前台壁纸用户表")
    @PostMapping
    public R delete(@RequestBody List<Long> ids) {
        return R.ok(this.userService.removeByIds(ids));
    }

    @ApiOperation(value = "重置用户密码为888888")
    @PostMapping("rePass")
    public R rePass(@RequestBody String id) {
        User user = userService.getById(id);
        String initialPassword = MD5Util.string2MD5(Const.INITIAL_PASSWORD);
        user.setPassword(initialPassword);
        return R.ok(this.userService.updateById(user));
    }
}

